package com.wangxiaonan.berry.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.wangxiaonan.berry.exception.ServiceException;
import com.wangxiaonan.berry.repository.MaxentRepository;
import com.wangxiaonan.berry.service.MaxentService;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @Author: wangxiaonan
 * @Date: 2019/7/3
 **/
@Service
public class MaxentServiceImpl implements MaxentService {

    @Value("${maxent-secret}")
    private String secret;

    @Resource
    private MaxentRepository maxentRepository;

    @Override
    public String query(String tick) {
        if (StringUtils.isEmpty(tick)) {
            throw new ServiceException("tick is null!");
        }
        JSONObject result = maxentRepository.query(secret, tick);
        if (result == null) {
            result = new JSONObject();
        }
        return result.toJSONString();
    }
}
